package Vistas;

import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import org.jvnet.substance.SubstanceLookAndFeel;

import Procesos.CargarDatos;

import javax.swing.ComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JInternalFrame;
import javax.swing.JTable;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Notas extends JInternalFrame implements PropertyChangeListener, ActionListener {

	private JPanel contentPane;
	private DefaultTableModel modelo;
	private JComboBox comboBox = new JComboBox();
	private JComboBox comboBox_1 = new JComboBox();
	private CargarDatos cargaCurso = new CargarDatos();
	private CargarDatos cargaAlumnos = new CargarDatos();
	private CargarDatos cabecera = new CargarDatos();
	private Object[][] datos ={{""}};
	protected String[] cursos = {"Desarrollo de Aplicaciones Multiplataforma","Administracion de Sistemas Informaticos en Red","Sistemas Microinformaticos y Redes"};
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame.setDefaultLookAndFeelDecorated(true);
					SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.BusinessBlackSteelSkin");
					Notas frame = new Notas();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Notas() {
		setMaximizable(true);
		setClosable(true);
		setTitle("Notas");
		setBounds(100, 100, 715, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 604, Short.MAX_VALUE)
					.addGap(10))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		JLabel lblCurso = new JLabel("Ciclo:");
		
		JLabel lblAlumno = new JLabel("Modulo:");	
		
		comboBox = new JComboBox(cursos);
		comboBox.addPropertyChangeListener(this);
		comboBox_1 = new JComboBox();
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton btnGuardar = new JButton("Guardar");
		btnGuardar.addActionListener(this);
		
		JButton btnMostrarAlumnos = new JButton("Mostrar Alumnos");
		btnMostrarAlumnos.setName("Mostrar");
		btnMostrarAlumnos.addActionListener(this);
		
		JButton btnNewButton_1 = new JButton("Nueva");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				InsertarNota nuevanota= new InsertarNota();
				nuevanota.setVisible(true);
			}
		});
		
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblCurso)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(comboBox, 0, 378, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblAlumno)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(btnMostrarAlumnos, GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
								.addComponent(btnGuardar, GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
								.addComponent(btnNewButton_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
					.addGap(0))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCurso)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblAlumno)
						.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(7)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(btnMostrarAlumnos)
									.addGap(38)
									.addComponent(btnNewButton_1)
									.addContainerGap())))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(37)
							.addComponent(btnGuardar)
							.addContainerGap())))
		);
		
		table = new JTable(datos, cabecera.getCabecera());
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
			},
			new String[] {
				"Nombre", "Apellidos", "Nota", "Comentarios"
			}
		) {
			Class[] columnTypes = new Class[] {
				Object.class, String.class, Integer.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		
		scrollPane.setViewportView(table);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
		
		if(comboBox.getSelectedItem().toString().equals(cursos[0].toString())){
			comboBox_1.removeAllItems();
			Object[]asignatura = cargaAlumnos.getAsignaturas(cursos[0].toString());
			for(int i = 0; i < asignatura.length; i++){
				comboBox_1.addItem(asignatura[i]);
			}
		}
		if(comboBox.getSelectedItem().toString().equals(cursos[1].toString())){
			comboBox_1.removeAllItems();
			Object[]asignatura = cargaAlumnos.getAsignaturas(cursos[1].toString());
			for(int i = 0; i< asignatura.length;i++){
				comboBox_1.addItem(asignatura[i]);
			}
		}
		if(comboBox.getSelectedItem().toString().equals(cursos[2].toString())){
			comboBox_1.removeAllItems();
			Object[]asignatura = cargaAlumnos.getAsignaturas(cursos[2].toString());
			for(int i = 0; i< asignatura.length;i++){
				comboBox_1.addItem(asignatura[i]);
			}	
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() instanceof JButton){
			JButton boton = (JButton)e.getSource();
			if(boton.getName().equals("Mostrar")){
				if(comboBox.getSelectedItem().toString().equals(cursos[0].toString())){
					Object[][]alumno = cargaAlumnos.cargarDatosAlumnos(cursos[0].toString());
					table.setModel(new DefaultTableModel(alumno, cabecera.getCabecera2()) {
						Class[] columnTypes = new Class[] {
								String.class, String.class, Integer.class, String.class
							};
							public Class getColumnClass(int columnIndex) {
								return columnTypes[columnIndex];
							}
						});
				}
				if(comboBox.getSelectedItem().toString().equals(cursos[1].toString())){
					Object[][] alumno = cargaAlumnos.cargarDatosAlumnos(cursos[1].toString());
					table.setModel(new DefaultTableModel(alumno, cabecera.getCabecera2())
					 {
						Class[] columnTypes = new Class[] {
							String.class, String.class, Integer.class, String.class
						};
						public Class getColumnClass(int columnIndex) {
							return columnTypes[columnIndex];
						}
					});
				}
				if(comboBox.getSelectedItem().toString().equals(cursos[2].toString())){
					Object[][]alumno = cargaAlumnos.cargarDatosAlumnos(cursos[2].toString());
					table.setModel(new DefaultTableModel(alumno, cabecera.getCabecera2()) {
						Class[] columnTypes = new Class[] {
								String.class, String.class, Integer.class, String.class
							};
							public Class getColumnClass(int columnIndex) {
								return columnTypes[columnIndex];
							}
						});
					}
			}else{
				this.dispose();
			}
		}
		
	}	
}

